package com.lakeworks.service.impl;

import com.lakeworks.domain.SysChannelAuth;
import com.lakeworks.mapper.SysChannelAuthMapper;
import com.lakeworks.service.ISysChannelAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static java.lang.String.valueOf;

/**
 * 渠道认证信息Service业务层处理
 *
 * @author lake
 * @date 2023-03-09
 */
@Slf4j
@Service
public class SysChannelAuthServiceImpl implements ISysChannelAuthService {
    @Resource
    private SysChannelAuthMapper sysChannelAuthMapper;

    @Resource(name = "JsonRedisTemplate")
    private RedisTemplate redisTemplate;

    @Override
    @Cacheable(value = "channelAuthAk", key = "#ak",unless = "#result == null")
    public SysChannelAuth selectByAk(String ak) {
        return sysChannelAuthMapper.queryByAk(ak);
    }

    @Override
    public void channelRedisInit() {
        log.info("渠道信息redis初始化........");

        sysChannelAuthMapper.selectSysChannelAuthList(new SysChannelAuth())
                .forEach(sysChannelAuth -> {
                    redisTemplate.opsForHash().put("channel:id", valueOf(sysChannelAuth.getChannelId()), sysChannelAuth);
                    redisTemplate.opsForHash().put("channel:ak", sysChannelAuth.getAk(), sysChannelAuth);
                });
    }

    /**
     * 查询渠道认证信息
     *
     * @param id 渠道认证信息主键
     * @return 渠道认证信息
     */
    @Override
    @Cacheable(value = "channelAuthId", key = "#id",unless = "#result == null")
    public SysChannelAuth selectByChannelId(Long id) {
        return sysChannelAuthMapper.selectByChannelId(id);
    }

    /**
     * 查询渠道认证信息列表
     *
     * @param sysChannelAuth 渠道认证信息
     * @return 渠道认证信息
     */
    @Override
    public List<SysChannelAuth> selectSysChannelAuthList(SysChannelAuth sysChannelAuth) {
        return sysChannelAuthMapper.selectSysChannelAuthList(sysChannelAuth);
    }

    /**
     * 新增渠道认证信息
     *
     * @param sysChannelAuth 渠道认证信息
     * @return 结果
     */
    @Override
    public int insertSysChannelAuth(SysChannelAuth sysChannelAuth) {
        return sysChannelAuthMapper.insertSysChannelAuth(sysChannelAuth);
    }

    /**
     * 修改渠道认证信息
     *
     * @param sysChannelAuth 渠道认证信息
     * @return 结果
     */
    @Override
    public int updateSysChannelAuth(SysChannelAuth sysChannelAuth) {
        return sysChannelAuthMapper.updateSysChannelAuth(sysChannelAuth);
    }

    /**
     * 批量删除渠道认证信息
     *
     * @param ids 需要删除的渠道认证信息主键
     * @return 结果
     */
    @Override
    public int deleteSysChannelAuthByIds(Long[] ids) {
        return sysChannelAuthMapper.deleteSysChannelAuthByIds(ids);
    }

    /**
     * 删除渠道认证信息信息
     *
     * @param id 渠道认证信息主键
     * @return 结果
     */
    @Override
    public int deleteSysChannelAuthById(Long id) {
        return sysChannelAuthMapper.deleteSysChannelAuthById(id);
    }
}
